package com.tecsup.log.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.tecsup.log.modelo.ProductoMaterial;
import com.tecsup.log.dao.ProductoMaterialDAO;
import excepcion.DAOExcepcion;

/**
 * Servlet implementation class ProductoMaterialInsertarServlet
 */
@WebServlet("/ProductoMaterialInsertarServlet")
public class ProductoMaterialInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductoMaterialInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int x2 = Integer.parseInt(request.getParameter("idProducto"));
		int x3 = Integer.parseInt(request.getParameter("idMaterial"));
		double x4 = Double.parseDouble(request.getParameter("unidades"));

		ProductoMaterial r = new ProductoMaterial();
		r.setIdProducto(x2);
		r.setIdMaterial(x3);
		r.setUnidades(x4);
		ProductoMaterialDAO dao = new ProductoMaterialDAO();
					
		
		
		try {
			dao.insertar(r);
			

						Collection<ProductoMaterial> lista = dao.buscarPorId(x2);
			// Guardar en el ambiente de request
			request.setAttribute("roles", lista);
			RequestDispatcher rd = request
					.getRequestDispatcher("/productoMaterial.jsp");
			rd.forward(request, response);
			
			
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("errorProductoMaterial.jsp");
			rd.forward(request, response);
		}
		
		
	}

}

